package com.andrew.markov.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.andrew.markov.gui.util.RowHeaderRenderer;
import com.andrew.markov.process.StateProbability;

public class ProbabilityTable extends JFrame {

	private static final long serialVersionUID = 8412729415011599206L;
	private JPanel contentPane;
	private JTable table;
	private JButton btnNewButton;
	public int matrixDimension;
	private JButton btnNewButton_1;
	private JTextField textField;

	/**
	 * Create the frame.
	 */

	public ProbabilityTable(String[][] fileArray) {
		System.out.println("Array hasd been loaded");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 654, 385);
		this.setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		matrixDimension = fileArray.length;
		System.out.println("matrix dimension is: " + fileArray.length);
		initTable(fileArray, matrixDimension);
	}

	public ProbabilityTable(int tableDimension) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 654, 385);
		this.setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		matrixDimension = tableDimension;

		Object[][] rowData = new Object[matrixDimension][matrixDimension + 1];
		for (int i = 0; i < matrixDimension; i++) {
			for (int j = 0; j < matrixDimension + 1; j++) {
				rowData[i][j] = "0";
				rowData[i][0] = "State " + (i);
			}
		}

		initTable(rowData, matrixDimension);
		// JScrollPane scrollPane = new JScrollPane();
		// scrollPane.setBounds(59, 11, 473, 230);
		// panel.add(scrollPane);
		//
		// table = new JTable();
		// scrollPane.setViewportView(table);

	}

	private void initTable(Object[][] rowData, int dimension) {
		System.out.println("StateProbability: initTable method");

		Object columnNames[] = new Object[dimension + 1];
		for (int i = 0; i < dimension + 1; i++) {
			columnNames[0] = "From \\ To";
			columnNames[i] = "State" + (i - 1);
		}
		System.out
				.println("Column names has been performed successfull. Length: "
						+ columnNames.length);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(47, 30, 478, 146);
		contentPane.add(scrollPane);

		table = new JTable(rowData, columnNames);

		System.out
				.println("Initialization of table has been finished successfull");
		table.setCellSelectionEnabled(true);
		table.setColumnSelectionAllowed(true);
		table.getColumnModel().getColumn(0)
				.setCellRenderer(new RowHeaderRenderer());// ROW HEADER
		scrollPane.setViewportView(table);

		btnNewButton = new JButton("POWER matrix");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// double[][] doubleMatrix = new
				// double[matrixDimension][matrixDimension];
				//
				// for (int i = 0; i < matrixDimension; i++) {
				// for (int j = 0; j < matrixDimension; j++) {
				// doubleMatrix[i][j] = Double.parseDouble((String) table
				// .getValueAt(i, j + 1));
				// // System.out.print(doubleMatrix[i][j] + "   ");
				//
				// }
				// // System.out.println();
				// }
				//
				// int step = Integer.parseInt(textField.getText());
				// double[][] superMatrix = StateProbability.performParkovSteps(
				// doubleMatrix, step);// STATIC_ACCESS
				// new StepPanel(superMatrix).setVisible(true);
				//
			}
		});
		btnNewButton.setBounds(42, 266, 189, 64);
		contentPane.add(btnNewButton);

		btnNewButton_1 = new JButton("Matrix in step # ");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// System.out.println(step);
			}
		});
		btnNewButton_1.setBounds(358, 266, 119, 64);
		contentPane.add(btnNewButton_1);

		textField = new JTextField();
		textField.setBounds(487, 277, 86, 42);
		contentPane.add(textField);
		textField.setColumns(10);
		// setVisible(true);
	}
}
